package com.book.shelves.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.book.business.entity.BookSell;
import com.book.business.service.BookSellService;
import com.book.common.annotation.ControllerEndpoint;
import com.book.common.annotation.Log;
import com.book.common.controller.BaseController;
import com.book.common.entity.FebsConstant;
import com.book.common.entity.FebsResponse;
import com.book.common.entity.QueryRequest;
import com.book.common.utils.BookMethod;
import com.book.common.utils.FebsUtil;
import com.book.shelves.entity.BookStore;
import com.book.shelves.entity.ShelvesApply;
import com.book.shelves.service.BookStoreService;
import com.book.shelves.service.ShelvesApplyService;
import com.book.warehouse.entity.WarehouseBook;
import com.book.warehouse.service.WarehouseBookService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @Author: Zhang Gaohe
 * @Date: 2021/5/6
 **/

@Controller
public class ShelvesSoldController extends BaseController {
    @Autowired
    ShelvesApplyService shelvesApplyService;
    @Autowired
    WarehouseBookService warehouseBookService;
    @Autowired
    BookStoreService bookStoreService;
    @Autowired
    BookSellService bookSellService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "shelves/shelvesSold")
    public String shelvesSoldIndex() {
        return FebsUtil.view("shelves/shelvesApply/shelvesSoldList");
    }

    @Log("架存-下架申请")
    @GetMapping("shelvesSold/list")
    @ResponseBody
    @RequiresPermissions("shelvesSold:view")
    public FebsResponse shelvesSoldIPage(QueryRequest request, BookSell bookSell) {
        Map<String, Object> dataTable = getDataTable(this.bookSellService.pageBookSell(request, bookSell, "1"));
        return new FebsResponse().success().data(dataTable);
    }

    @Log("架存-下架审批")
    @PostMapping("shelvesSold/yes/{ids}")
    @ResponseBody
    @RequiresPermissions("shelvesSold:yes")
    @ControllerEndpoint(operation = "审批成功", exceptionMessage = "审批失败")
    public FebsResponse shelvesApplyYes(@PathVariable String ids) {
        List<String> list = Arrays.asList(ids.split(","));
        List<BookSell> bookSellList = bookSellService.list(new QueryWrapper<BookSell>().lambda().in(BookSell::getId, list));
        for (int i = 0; i < bookSellList.size(); i++) {
            bookStoreService.remove(new QueryWrapper<BookStore>().lambda()
                    .eq(BookStore::getBookname, bookSellList.get(i).getBookname()).notIn(BookStore::getFlag, "1"));
            warehouseBookService.update(new UpdateWrapper<WarehouseBook>().lambda()
                    .eq(WarehouseBook::getBookname, bookSellList.get(i).getBookname())
                    .eq(WarehouseBook::getPutaway, "1").set(true, WarehouseBook::getPutaway, "0"));
        }
        bookSellService.removeByIds(list);
        shelvesApplyService.remove(new QueryWrapper<ShelvesApply>().lambda().in(ShelvesApply::getBookid, list));
        return new FebsResponse().success();
    }

    @Log("架存-下架驳回")
    @PostMapping("shelvesSold/no/{ids}")
    @ResponseBody
    @RequiresPermissions("shelvesApply:no")
    @ControllerEndpoint(operation = "驳回成功", exceptionMessage = "驳回失败")
    public FebsResponse shelvesApplyNo(@PathVariable String ids) {
        List<String> list = Arrays.asList(ids.split(","));
        shelvesApplyService.remove(new QueryWrapper<ShelvesApply>().lambda().in(ShelvesApply::getBookid, list));
        return new FebsResponse().success();
    }
}
